// pages/todolist/todolist.js
Page({
  data: {
    //文本框的数据模型
    input:'',
    //任务清单数据模型
    todos:[
      {name:'Learning WEAPP',completed:false},
      {name:'Learning JavaScript',completed:true},
      {name:'Learning HTML',completed:false},
    ],
    leftCount:2,//未选中个数
    allCompleted:false,//选中状态
  },



  inputHandle(e){
    console.log(e.detail.value)

    let val = e.detail.value
    console.log(val);
    
    let obj = {
      name: val,
      completed:false
    }

    this.data.todos.push(obj)

    this.setData({
      todos:this.data.todos,
      input:'',
      leftCount:this.data.leftCount+1
    })
  },

  delet(e){

    console.log(e.target.dataset.index)

    var i = e.target.dataset.index;
    console.log(i);

    var data2 = this.data.todos;
    console.log(data2);

    data2.splice(i,1)

    this.setData({
      todos:data2
    })

},

togglehadle:function(e){
  var item=this.data.todos[e.currentTarget.dataset.index]
  item.completed=!item.completed
  var leftcount = this.data.leftCount+ (item.completed ?-1:1)
  this.setData({
   todos:this.data.todos,
   leftCount:leftcount,
  })
},

changeState:function(){
  this.data.allCompleted = !this.data.allCompleted
  var todos = this.data.todos
  var that = this
  console.log(that);

  todos.forEach(function(item){
    console.log(item);
    item.completed=that.data.allcompleted
    console.log(item.completed);
  })

  var leftcount = this.data.allcompleted?0:this.data.todos.length
  this.setData({
    todos:todos,
    leftCount:leftcount
  })
},

clearList:function(){
  var todos = this.data.todos.filter(function(item){
    console.log(item.completed);
    return !item.completed
  })

  this.setData({
    todos:todos
  })
}
 
})